import urls from "../../common/js/api_url_array";

export default {
	data() {
		return {
			//数据
			uploadNum: 0,
      ue: {},
			validate: {}, //验证对象
			policyRelease: {}, //政策对象
			defaultUrl:require("../../images/ExpertsShow/bg-img.png"),//默认上传图片
			showMsg:false,//图片提示是否显示
      tempName: '',
			fatherIndex: 0, //一级菜单index
		}
	},
	created() { //调用方法

	},
	updated:function(){
		if(this.getParamValue("fatherIndex")){
			this.fatherIndex=this.getParamValue("fatherIndex");
		}
		if(this.getParamValue("childIndex")){
			this.childIndex=this.getParamValue("childIndex");
		}
	},
	mounted() {
		let that = this;
		UE.delEditor('editor');
    that.ue = UE.getEditor('editor', {
			maximumWords: 10000
		});
    that.ue.addListener( 'contentChange', function( editor ) {
      $("textarea[name='content']").val(that.ue.getContent());
      $("textarea[name='content']").valid();
    });
		UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
		UE.Editor.prototype.getActionUrl = function(action) {
			if(action == 'uploadimage' || action == 'uploadscrawl' || action == 'uploadimage') {
				return that.urls.uploadUediter;
			} else if(action == 'uploadvideo') { //视频
				return that.urls.uploadUediter;
			} else if(action == 'uploadfile') { //附件
				return that.urls.uploadUediter;
			} else {
				return this._bkGetActionUrl.call(this, action);
			}
		};
		let uuid = this.get("publishContentid", uuid);
		if(uuid != null && uuid != "") {
			that.$post(
					that.urls.getPolicyRelease, {
						"uuid": uuid
					}
				)
				.then(function(res) {
					if(res.success) {
						that.policyRelease = res.data;
						if (res.data != null) {
              that.tempName = res.data.name;
            }
            $(".showHandIdFront").attr("src", that.urls.baseUrl + res.data.url);
            $(".handIdFrontA").css("display", "none");
            $("input[name='coverUrl']").val(res.data.url);
            that.ue.ready(function() {
              that.ue.setContent(that.policyRelease.content);
						});
					}
				})
		}

		//校验名称重复
		$.validator.addMethod("checkNameRepeat", function(value, element, params) {
			let flg = false;
			if(that.policyRelease.id != null && (value == that.tempName)) {
				flg = true;
			} else {
				$.ajax({
					type: "POST",
					async: false,
					url: that.urls.checkPolicyReleaseName,
					data: JSON.stringify({
						"name": value
					}),
					dataType: "json",
					contentType: "application/json",
					success: function(data) {
						if(data.success) {
							flg = !data.data;
						}
					}
				});
			}
			return this.optional(element) || flg;
		}, "该标题已存在！");

    //校验富文本类容
    $.validator.addMethod("checkUEContent", function(value, element, params) {
      let len = that.ue.getContentTxt().length;
      return this.optional(element) || (len > 0 && len < 10000);
    }, "正文最多只能输入0-10000个字！");

		//加载表单验证
		that.validate = $("#policyReleaseForm").validate({
			ignore: [],
			rules: {
				name: { // 政策发布名称
					required: true,
					maxlength: 50,
          checkNameRepeat: true
				},
				type: { // 政策发布类型
					required: true
				},
				coverUrl: { // 政策发布封面图片
					required: true,
					maxlength: 1000
				},
				content: { // 政策发布内容
					required: true
				}
			},
			messages: {
				name: { // 政策发布名称
					required: "请输入政策标题",
					maxlength: "标题最多只能输入50个字"
				},
				type: { // 政策发布类型
					required: "请选择政策类型"
				},
				coverUrl: { // 政策发布封面图片
					required: "请选择展示图",
					maxlength: "封面图片最多只能输入1000个字"
				},
				content: { // 政策发布内容
					required: "请输入内容"
				}
			},
//			errorElement: "em",
//			errorPlacement: function(error, element) {
//				error.appendTo(element.parents(".error-box").children(".error-msg"));
//			}
		});
	},
	methods: { //写函数方法
		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#handIdFront").click();
			}
		},
		idFrontImg: function(event) {
			var that = this;
			var file = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(file)) {
        return;
      }
			let formData = new FormData();
			formData.append('fileNames', file);
			let config = {
				headers: {
					'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
				}
			};
			that.$post(
					that.urls.upload,
					formData,
					config
				)
				.then(function(res) {
					if(res.success) {
						let fileInfo = res.data[0];
						$(".showHandIdFront").attr("src", that.urls.baseUrl + fileInfo.url);
						$(".handIdFrontA").css("display", "none");
						$("input[name='coverUrl']").val(fileInfo.url);
						that.policyRelease.coverUrl=fileInfo.url;
						that.showMsg=false;
					}
				})
		},
		//保存
		saveData: function() {
			let that = this;
			let flg = that.validate.form();
			$("#coverUrl-error").css("display","none");
			that.showMsg=true;
			if(flg) {
				let dataObj = combinationFormObject(that.validate.currentForm);
				that.$post(
						that.urls.addPolicyRelease,
						dataObj
					)
					.then(function(res) {
						if(res.success) {
							that.policyRelease = res.data;
							layer.msg("发布成功！");
							that.pressRelease();
						} else {
							layer.msg("发布失败！");
						}
					})
			}
		},
		//修改
		updateData: function() {
			let that = this;
			let flg = that.validate.form();
			if(flg) {
				let dataObj = combinationFormObject(that.validate.currentForm);
				that.$post(
						that.urls.updatePolicyRelease,
						dataObj
					)
					.then(function(res) {
						if(res.success) {
							layer.msg("保存成功！");
							that.pressRelease();
						} else {
							layer.msg("保存失败！");
						}

					})
			}
		},
		//返回政策列表页
		pressRelease: function() {
			var that = this;
			that.$router.push({
				path: "policyIssued?fatherIndex="+this.fatherIndex,
			})
		},
	}
}
